package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.thread.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.CountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {
    @Autowired
    private MemberReservationMapper reservationMapper;

    @Override
    public int getCancelledCount(Long userId, LocalDateTime startOfDay, LocalDateTime endOfDay) {
        log.warn("================>startOfDay:{},endOfDay:{}", startOfDay, endOfDay);
        String startOfDayStr = String.valueOf(startOfDay);
        String endOfDayStr = String.valueOf(endOfDay);
        startOfDayStr = startOfDayStr.replace("T", " ");
        endOfDayStr = endOfDayStr.replace("T", " ");
        log.warn("================>startOfDayStr:{},endOfDayStr:{},userId:{}", startOfDayStr, endOfDayStr, userId);

        int count = reservationMapper.getCancelledCount(userId, startOfDayStr, endOfDayStr);
        return count;
    }

    @Override
    public void add(ReservationDto dto) {
//        Reservation reservation = new Reservation();
//        BeanUtils.copyProperties(dto, reservation);
//
//        DateTimeFormatter datetTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime ldtTime = LocalDateTime.parse(dto.getTime(), datetTimeFormatter);
//        reservation.setTime(ldtTime);
//        reservation.setStatus(0);
//        reservation.setCreateTime(LocalDateTime.now());
//        reservation.setCreateBy(UserThreadLocal.getUserId());
//        reservationMapper.add(reservation);
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        reservation.setStatus(0);
        this.save(reservation);
    }

    @Override
    public List<Reservation> page(Integer status) {
        List<Reservation> reservations = reservationMapper.page(status);
        return reservations;
    }

    @Override
    public void cancel(Long id) {
//        LocalDateTime updateTime = LocalDateTime.now();
        Reservation reservation = this.getById(id);
        reservation.setStatus(2);
        this.updateById(reservation);
    }

    /**
     * 定时更新预约状态
     */
    @Override
    public void updateReservationStatus() {
        // 查询状态为0(待报到)且预约时间早于当前时间的预约,更新为3(已过期)
        List<Reservation> reservationList = this.list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now())
        );
        // 为空直接返回
        if (CollectionUtils.isEmpty(reservationList)) {
            return;
        }
        // 更新状态为3
        reservationList.forEach(reservation -> {
            reservation.setStatus(3);
        });
        // 更新数据库
        this.updateBatchById(reservationList);
    }

    @Override
    public List<CountVo> getCountByTime(LocalDateTime startOfDay,LocalDateTime endOfDay) {
        List<CountVo> result = reservationMapper.getCountByTime(startOfDay,endOfDay);

        return result;
    }
}
